﻿using System.Collections.Generic;
using Netkk.Common.Interfaces.Repositories;
using Netkk.Common.Interfaces.Services;
using Netkk.Common.Models;
using System;
using System.Linq;

namespace Netkk.Library.Services
{
    public class DownloadService : IDownloadService
    {
        private readonly IDownloadRepository repository;

        public DownloadService(IDownloadRepository repository)
        {
            this.repository = repository;
        }

        public IEnumerable<Download> GetAllDownloads()
        {
            return repository.Select();
        }

        public Download GetDownloadById(Guid id)
        {
            IEnumerable<Download> downloads = repository.Select();
            Download link = (from d in downloads
                         where d.Id == id
                         select d).FirstOrDefault();
            return link;
        }

        public bool SaveDownload(Download download)
        {
            try
            {
                repository.Save(download);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void DeleteDownload(Download download)
        {
            repository.Delete(download);
        }

    }

}
